<?php
/**
 * Class that operate on table 'bible_book_names'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-11-21 10:54
 */
abstract class BibleBookNamesMySqlDAO implements BibleBookNamesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return BibleBookNamesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM bible_book_names WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM bible_book_names';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM bible_book_names ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param bibleBookName primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM bible_book_names WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param BibleBookNamesMySql bibleBookName
 	 */
	public function insert($bibleBookName){
		$sql = 'INSERT INTO bible_book_names (language, book_no, book_type, long_name, short_name, middle_name, chapter_size, verse_size) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($bibleBookName->language);
		$sqlQuery->setNumber($bibleBookName->bookNo);
		$sqlQuery->set($bibleBookName->bookType);
		$sqlQuery->set($bibleBookName->longName);
		$sqlQuery->set($bibleBookName->shortName);
		$sqlQuery->set($bibleBookName->middleName);
		$sqlQuery->setNumber($bibleBookName->chapterSize);
		$sqlQuery->setNumber($bibleBookName->verseSize);

		$id = $this->executeInsert($sqlQuery);	
		$bibleBookName->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param BibleBookNamesMySql bibleBookName
 	 */
	public function update($bibleBookName){
		$sql = 'UPDATE bible_book_names SET language = ?, book_no = ?, book_type = ?, long_name = ?, short_name = ?, middle_name = ?, chapter_size = ?, verse_size = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($bibleBookName->language);
		$sqlQuery->setNumber($bibleBookName->bookNo);
		$sqlQuery->set($bibleBookName->bookType);
		$sqlQuery->set($bibleBookName->longName);
		$sqlQuery->set($bibleBookName->shortName);
		$sqlQuery->set($bibleBookName->middleName);
		$sqlQuery->setNumber($bibleBookName->chapterSize);
		$sqlQuery->setNumber($bibleBookName->verseSize);

		$sqlQuery->setNumber($bibleBookName->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM bible_book_names';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByLanguage($value){
		$sql = 'SELECT * FROM bible_book_names WHERE language = ? ORDER BY BOOK_NO';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBookNo($value){
		$sql = 'SELECT * FROM bible_book_names WHERE book_no = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBookType($value){
		$sql = 'SELECT * FROM bible_book_names WHERE book_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLongName($value){
		$sql = 'SELECT * FROM bible_book_names WHERE long_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByShortName($value){
		$sql = 'SELECT * FROM bible_book_names WHERE short_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMiddleName($value){
		$sql = 'SELECT * FROM bible_book_names WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByChapterSize($value){
		$sql = 'SELECT * FROM bible_book_names WHERE chapter_size = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVerseSize($value){
		$sql = 'SELECT * FROM bible_book_names WHERE verse_size = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByLanguage($value){
		$sql = 'DELETE FROM bible_book_names WHERE language = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBookNo($value){
		$sql = 'DELETE FROM bible_book_names WHERE book_no = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBookType($value){
		$sql = 'DELETE FROM bible_book_names WHERE book_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLongName($value){
		$sql = 'DELETE FROM bible_book_names WHERE long_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByShortName($value){
		$sql = 'DELETE FROM bible_book_names WHERE short_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMiddleName($value){
		$sql = 'DELETE FROM bible_book_names WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByChapterSize($value){
		$sql = 'DELETE FROM bible_book_names WHERE chapter_size = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVerseSize($value){
		$sql = 'DELETE FROM bible_book_names WHERE verse_size = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return BibleBookNamesMySql 
	 */
	protected function readRow($row){
		$bibleBookName = new BibleBookName();
		
		$bibleBookName->id = $row['id'];
		$bibleBookName->language = $row['language'];
		$bibleBookName->bookNo = $row['book_no'];
		$bibleBookName->bookType = $row['book_type'];
		$bibleBookName->longName = $row['long_name'];
		$bibleBookName->shortName = $row['short_name'];
		$bibleBookName->middleName = $row['middle_name'];
		$bibleBookName->chapterSize = $row['chapter_size'];
		$bibleBookName->verseSize = $row['verse_size'];

		return $bibleBookName;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return BibleBookNamesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>